package com.jzoom.zoom.admin.models;

import com.jzoom.zoom.admin.daos.ModDao;
import com.jzoom.zoom.caster.Caster;
import com.jzoom.zoom.common.annotations.Inject;
import com.jzoom.zoom.dao.Ar;
import com.jzoom.zoom.web.exception.StatusException;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class MutilDaoListener implements DaoListener {
	
	@Inject
	ModDao moduleDao;

	
	/**
	 * 角色权限管理
	 * @return 
	 */
	private String getFinalMods(Map<String, Object> data) {
		String mods = (String) data.get("mods");
		String[] parts = mods.split(",");
		//对于parts的每一个元素来说，都要找到通往顶级元素路径上面的所有元素，如果没有，则在增加这个元素。
		Set<String> set = new HashSet<String>();
		for (String part : parts) {
			set.add(part);
		}
		
		Map<String, String> id2parentMap = moduleDao.getIdToParentMap();
		
		List<String> added = new ArrayList<String>();
		Iterator<String> it = set.iterator();
		while(it.hasNext()) {
			String key = it.next();
			while(true) {
				String pId = id2parentMap.get(key);
				if(pId==null) {
					break;
				}
				key = pId;
				if(!set.contains(pId) && !"0".equals(pId)) {
					added.add(pId);
				}
			}
		}
		
		if(added.size() > 0) {
			for (String add : added) {
				set.add(add);
			}
			return StringUtils.join(set,",");
		}
		
	
		return mods;
	}
	

	@Override
	public void beforeAdd(Ar ar, String table, Map<String, Object> data) {

	}

	@Override
	public void onAdd(Ar ar, String table, Map<String, Object> data) {
		if ("sys_user".equals(table)) {
			String account = (String) data.get("account");
			String userId = ar.table(table).where("account", account).getValue("id", String.class);

			ar.table(table).set("pwd", AccessService.encrypt(userId, (String) data.get("pwd")))
					.where("id", userId).update();
		}
	}

	@Override
	public void beforeUpdae(Ar ar, String table, Object id, Map<String, Object> data) {

	}

	@Override
	public void onUpdate(Ar ar, String table, Object id, Map<String, Object> data) {

	}


	@Override
	public void beforeModify(Ar ar, String table, Object id, Map<String, Object> data) {
		if (table.equals("sys_component")) {
			data.put("update_at", System.currentTimeMillis() / 1000);
		} else if (table.equals("sys_role")) {
            data.put("role", "ADMIN");
			data.put("mods", getFinalMods(data));
		} else if (table.equals("sys_page")) {
			//手动操作
			data.put("manual", 1);
			contentSercie.updatePageForUrl((String) data.get("url"));
		} else if (table.equals("sys_dic")) {
			configService.update((String) data.get("name"));
		} else if ("sys_user".equals(table)) {
			if (id != null) {
				String userId = Caster.to(id, String.class);
				data.put("pwd", AccessService.encrypt(userId, (String) data.get("pwd")));
			}
            data.put("role", "ADMIN");
        }
	}

	@Inject
	private ContentSercie contentSercie;

	@Inject
	private ConfigService configService;

	@Override
	public void beforeDel(Ar ar, String table, Object id) {

	}

	@Override
	public void onDel(Ar ar, String table, Object id) {
		if (table.equals("sys_mod")) {
			if (ar.table(table).where("p_id", id).getCount() > 0) {
                throw new StatusException.ApiError("不能删除本节点，需要先删除子节点");
            }
		}
	}


}
